import type { LoadingInstance } from 'element-plus/lib/components/loading/src/loading.js'
import { ElMessageBox, ElLoading, ElMessage } from 'element-plus'

import type { IAlertInfo } from './type'

type messageType = 'success' | 'warning' | 'error' | 'info'

class ToastUtil {
  public static loadingDialog: LoadingInstance

  /**
   * 1:弹窗,能展示取消,确认按钮
   * @param info 弹窗要展示的信息
   * @returns true:点了确认  false:点了取消
   */
  public static alert(info: IAlertInfo): Promise<boolean> {
    return new Promise((resolve) => {
      ElMessageBox.alert(info.message ?? '', info.title ?? '', {
        cancelButtonText: info.cancelButtonText ?? '取消',
        confirmButtonText: info.confirmButtonText ?? '确定',
        showCancelButton: info.showCancelButton ?? false,
        callback: (value: any) => {
          console.log('操作类型是', value)
          if (value === 'confirm') {
            resolve(true)
          } else {
            resolve(false)
          }
        }
      })
    })
  }

  /**
   * 2:展示错误信息
   * @param message 要展示的信息
   */
  public static showError(message: string) {
    this.showMessage(message, 'error')
  }

  /**
   * 3:展示成功信息
   * @param message  要展示的信息
   */
  public static showSuccess(message: string) {
    this.showMessage(message, 'success')
  }

  /**
   * 4:展示信息
   * @param message  信息
   * @param type  类型
   */
  public static showMessage(message: string, type?: messageType) {
    ElMessage({
      message: message,
      type: type ?? 'info'
    })
  }

  /**
   * 5:展示加载loading
   * @param message  信息
   */
  public static showLoading(message = '请求中....') {
    this.loadingDialog = ElLoading.service({
      text: message,
      lock: true,
      background: 'rgba(0,0,0,0.5)'
    })
  }

  /**
   * 6:关闭loading
   */
  public static closeLoading() {
    this.loadingDialog?.close()
  }
}

export { ToastUtil }

export default ToastUtil
